<template>
  <div class="_wz_maintenance">
    <Card dis-hover :bordered="true">
      <template #title>搜索</template>
      <Form ref="formSearch" :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="车牌号">
          <i-input v-model="formDataSearch.vehiclePlateNo" placeholder="请填写车牌号" clearable/>
        </FormItem>
        <FormItem label="车主业户">
          <i-input v-model="formDataSearch.ownerName" placeholder="请填写车主业户" clearable/>
        </FormItem>
        <FormItem label="登记人">
          <i-input v-model="formDataSearch.registrantName" placeholder="请填写登记人" clearable/>
        </FormItem>
        <FormItem label="到期日期">
          <DatePicker type="daterange" v-model="dateRange" placeholder="请选择到期日期" style="width:96%"></DatePicker>
        </FormItem>
        <FormItem label="到期状态">
          <i-select v-model="formDataSearch.expireState" filterable placeholder="请选择到期状态" clearable>
            <Option value="1">未到期</Option>
            <Option value="0">已到期</Option>
            <Option value="2">即将到期</Option>
          </i-select>
        </FormItem>
        <FormItem label="提醒状态">
          <i-select v-model="formDataSearch.whetherRemind" filterable placeholder="请选择提醒状态" clearable>
            <Option value="1">开启</Option>
            <Option value="0">关闭</Option>
          </i-select>
        </FormItem>
        <FormItem label="经营企业">
          <i-input v-model="formDataSearch.deptName" placeholder="请输入经营企业" clearable />
        </FormItem>


        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true" style="height: 100%;overflow: hidden;margin: 10px 0 0;">
      <template #title>列表</template>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>



    <Drawer :title="formTitle" v-model="formOperation" width="1000" :draggable="true">
      <maintenanceDetails v-if="formOperation" :selectData="selectData"></maintenanceDetails>
    </Drawer>

  </div>
</template>
<script>


import {resolveComponent,getCurrentInstance,inject,nextTick,reactive, toRefs,toRef,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted, ref,defineComponent,toRaw,watch} from 'vue'
import { useRouter,useRoute } from "vue-router";

import { useMainStore } from '../../../store'  //pinia 使用 类似 vuex
import { storeToRefs } from 'pinia'//pinia 使用 类似 vuex
const mainStore = useMainStore() //pinia 使用 类似 vuex


import wzTbale from "../../../components/table/index.vue";
import {systemDictList} from "../../../util/commonInterfaces.js";//字典接口
import maintenanceDetails from "../facility/maintenanceDetails.vue";//设备设施 台账详情

//reactive
export default defineComponent({
  name: 'maintenance',//组件名称
  components: {
    wzTbale,//表格
    maintenanceDetails,//设备设施 台账详情
  },//组件引用
  setup (props,context) {


    const apiUrl = inject('$apiUrl'); // 变量全局化 后台地址
    const $axios = inject('$axiosInterceptor');// 变量全局化 axios
    const $ViewUIPlus = inject('$ViewUIPlus');//引用全局组件
    const router = useRouter();//路由跳转使用
    const $common = inject('$common');//自己封装的方法

    // 用于获取$refs
    const wzTbale = ref();


    const state = reactive({
      formDataSearch:{},//搜索条件
      dateRange: [],//时间范围使用
      tableData: {
        url: apiUrl.managementMaintainMaintainPageList,//请求地址
        method: 'post',//请求方式
        where: {},//搜索条件
        height: '100%',//表格高度 auto
        page: true,//分页是否使用
        columns: [//表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          { title: "档案号", minWidth: 190, key: "maintainRecordNumber" ,tooltip:true,resizable: true},
          { title: "车牌号", minWidth: 120, key: "maintainVehiclePlateNo",tooltip:true,resizable: true },
          { title: "车主业户", minWidth: 120, key: "maintainVehicleOwnerName",tooltip:true,resizable: true },
          {title: "联系电话", minWidth: 140, key: "maintainVehicleOwnerPhone",tooltip:true,resizable: true},
          {
            title: "维保等级", minWidth: 120,tooltip:true,resizable: true,
            render: (h, params) => {
              return h("span", params.row.maintainType == 1 ? "一级维保" : "二级维保");
            }
          },
          { title: "办理日期", minWidth: 120, key: "maintainDate",tooltip:true,resizable: true },
          {
            title: "维保周期", minWidth: 120,tooltip:true,resizable: true,
            render: (h, params) => {
              return h("span", params.row.maintainCycle + "天");
            }
          },
          { title: "到期日期", minWidth: 120, key: "maintainExpireDate",tooltip:true,resizable: true },
          { title: "登记人", minWidth: 120, key: "maintainRegistrantName",tooltip:true,resizable: true },
          { title: "经营企业", minWidth: 260, key: "deptName" ,tooltip:true,resizable: true},
          { title: "创建时间", minWidth: 190, key: "createTime" ,tooltip:true,resizable: true},
          { title: "修改时间", minWidth: 190, key: "updateTime",tooltip:true,resizable: true },
          {
            title: "提醒状态", minWidth: 120,tooltip:true,resizable: true,
            render: (h, params) => {
              return h(resolveComponent('Tag'), {type:"dot",color: params.row.maintainWhetherRemind == 0 ? "error" : "primary"},  params.row.maintainWhetherRemind == 0 ? "关闭" : "开启");
            }
          },
          {
            title: "到期状态", minWidth: 140,tooltip:true,resizable: true,
            render: (h, params) => {
              return h(resolveComponent('Tag'), {type:"dot",
                color:  params.row.maintainExpireState == 0 ? "error" : params.row.maintainExpireState == 1 ? "success" : "warning"},
                  params.row.maintainExpireState == 0 ? "已到期" : params.row.maintainExpireState == 1 ? "未到期" : "即将到期");
            }
          },
          {
            title: "档案状态", minWidth: 120,tooltip:true,resizable: true,
            render: (h, params) => {
              if (params.row.maintainState == 1) {
                return h(resolveComponent('Tag'), {type:"dot",color: "success"}, "正常");
              } else if (params.row.maintainState == 2) {
                return h(resolveComponent('Tag'), {type:"dot",color: "warning"}, "过户");
              } else if (params.row.maintainState == 3) {
                return h(resolveComponent('Tag'), {type:"dot",color: "error"}, "报废");
              } else {
                return h(resolveComponent('Tag'), {type:"dot",color: "default"}, "未知");
              }
            }
          },
          {
            title: '操作', width: 80, fixed: 'right', render: (h, params) => {

              var operationButtons = [];
              operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    onClick: () => {

                      state.selectData = JSON.parse(JSON.stringify(params.row));
                      state.formTitle = '详情';
                      state.formOperation = true;
                    }
                  }, "查看")
              )
              return operationButtons;
            }
          },
        ],
        pagingParameters:{// 分页参数
          page:'pageNo',
          limit:'pageSize'
        },
        format: {//返回数据格式
          list: ['data'],//数据list
          count: ['count'],//数据总量
        },
      },


      selectData:{},//选中数据
      formOperation:false,//弹框状态
      formTitle:'',//弹框标题
    });



    //搜索方法
    const searchSubmit = ()=>{
      let formDataSearch = JSON.parse(JSON.stringify(state.formDataSearch));
      formDataSearch.startDate = !!state.dateRange[0] ? $common.timeFormatting(state.dateRange[0], "yyyy-MM-dd") : "";
      formDataSearch.endDate = !!state.dateRange[1] ? $common.timeFormatting(state.dateRange[1], "yyyy-MM-dd") : "";
      state.tableData.where = formDataSearch;
      wzTbale.value.tableLoadData(true)

    }




    // 其他的生命周期
    onBeforeMount (() => {//beforeMount挂载前状态 // console.log("App ===> 相当于 vue2.x 中 beforeMount")

    })
    onMounted (async() => {//mounted 挂载结束状态  // console.log("App ===> 相当于 vue2.x 中 mounted")

    })

    // 注意，onBeforeUpdate 和 onUpdated 里面不要修改值
    onBeforeUpdate (() => {//beforeUpdate 更新前状态 // console.log("App ===> 相当于 vue2.x 中 beforeUpdate")

    })

    onUpdated (() => {//updated 更新完成状态 // console.log("App ===> 相当于 vue2.x 中 updated")

    })

    onBeforeUnmount (() => {//beforeDestroy 销毁前状态  // console.log("App ===> 相当于 vue2.x 中 beforeDestroy")
    })

    onUnmounted (() => {//destroyed 销毁完成状态  // console.log("App ===> 相当于 vue2.x 中 destroyed")
    })

    return {
      ...toRefs(state),
      searchSubmit,//搜索方法
      wzTbale,// ref 使用 封装的表格
    }
  }
})


</script>
<style lang="less">
._wz_maintenance{
  padding: 10px;
  height: 100%;
  display: flex;
  flex-direction: column;

  .ivu-card-body {
    height: calc(100% - 51px);
  }
}
</style>
